import {BaseNodeModel} from "@logicflow/core/src/model/node";

// 当前节点只能有一个输出边
export const sourceRulesOnlyOneOut = {
    message: '开始节点只能有一个输出边',
    validate: (source: BaseNodeModel, target: BaseNodeModel) => {
        // 获取当前图表中所有的边
        const edges = source.graphModel.edges;
        if (!edges) {
            return true;
        }
        // 过滤出所有的出边
        const outgoingEdges = edges.filter((edge) => edge.sourceNodeId === source.id);
        return outgoingEdges.length == 0;
    },
};

// 当前节点不能有输出边
export const sourceRulesNotOut = {
    message: "结束节点只能连入，不能连出！",
    validate: (source: BaseNodeModel, target: BaseNodeModel, sourceAnchor, targetAnchor) => {
        let isValid = true;
        if (source) {
            isValid = false;
        }
        return isValid;
    },
};

// 当前节点为目标节点不能有输入边
export const targetRulesNotIn = {
    message: "起始节点不能作为边的终点",
    validate: () => false,
};

// 当前节点不能有指向相同的边
export const sourceRulesNotSameOut = {
    message: '当前节点不能有指向相同的边',
    validate: (source: BaseNodeModel, target: BaseNodeModel) => {
        // 获取当前图表中所有的边
        const edges = source.graphModel.edges;
        if (!edges) {
            return true;
        }
        // 过滤出所有的出边
        const outgoingEdges = edges.filter((edge) =>
            edge.sourceNodeId === source.id && edge.targetNodeId === target.id
        );
        return outgoingEdges.length == 0;
    },
};